package com.example.proyyj.Controller;

import com.example.proyyj.Entity.Building;
import com.example.proyyj.Entity.Calligraphy;
import com.example.proyyj.Entity.ResultEntity;
import com.example.proyyj.Service.BuildingService;
import com.example.proyyj.Service.CalligraphyService;
import com.example.proyyj.Util.JSONTool;
import com.example.proyyj.tool.JsonUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/Calligraphy/")
public class CalligraphyController {
    @Resource
    CalligraphyService calligraphyService;
    ResultEntity resultEntity=null;

    //按id查询书画
    @GetMapping("getCalligraphy")
    private String getCalligraphyById(@RequestParam("id")int id){
        resultEntity=new ResultEntity();
        try{
           Calligraphy calligraphy =calligraphyService.getCalligraphyById(id);
            if(calligraphy==null){
                resultEntity.setCode("1");
                resultEntity.setMsg("查无此人");
            }else {
                resultEntity.setCode("0");
                resultEntity.setMsg("查询成功");
                resultEntity.setData(calligraphy);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 查询所有书画
     * @param
     * @return
     */
    @GetMapping("getCalligraphyList")
    private  String calligraphy(){

        try {
            resultEntity=new ResultEntity();
            List<Calligraphy> list=calligraphyService.getCalligraphyList();
            if (list!=null){
                resultEntity.setCode("0");
                resultEntity.setMsg("查询成功");
                resultEntity.setCount(list.size());
                resultEntity.setData(list);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("99999");
            resultEntity.setMsg("查询失败"+e.getMessage());
        }
        return  JSONTool.getJsonTool().toJson(resultEntity);
    }
}
